package com.boful.convert.model;

import java.io.File;
import java.io.Serializable;

import org.apache.commons.codec.binary.Hex;

import com.boful.common.file.utils.FileType;
import com.boful.common.file.utils.FileUtils;

public class DiskFile extends File implements Serializable{
	private static final long serialVersionUID = 1L;

	public DiskFile(String pathname) {
		super(pathname);
	}

	public DiskFile(File file) {
		super(file.getAbsolutePath());
	}

	/**
	 * 获取文件hash
	 * 
	 * @return 文件hash十六进制大写
	 */
	public String getHash() {
		return Hex.encodeHexString(FileUtils.getHash(this)).toUpperCase();
	}

	public boolean isVideo() {
		return FileType.isVideo(getName());
	}

	public boolean isDocument() {
		return FileType.isDocument(getName());
	}

	public boolean isAudio() {
		return FileType.isAudio(getName());
	}

	public boolean isImage() {
		return FileType.isImage(getName());
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof File || obj instanceof DiskFile) {
			File file = (File) obj;
			String hash1 = Hex.encodeHexString(FileUtils.getHash(file))
					.toUpperCase();
			String hash2 = this.getHash();
			return hash1.equals(hash2);
		} else {
			return false;
		}
	}

	public String getFileType() {
		return FileUtils.getFileSufix(getName());
	}

	public String getFileName() {
		int end = getName().length() - getFileType().length();
		return getName().substring(0, end-1);
	}

	@Override
	public String toString() {
		return "DiskFile [getName()=" + getName()
				+ ", getAbsolutePath()=" + getAbsolutePath() + "]";
	}

}
